<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="学生学号" prop="num">
        <el-input
          v-model="dataForm.num"
          placeholder="违规学生学号"
        ></el-input>
      </el-form-item>
      <el-form-item label="学生姓名" prop="realName">
        <el-input
          v-model="dataForm.realName"
          placeholder="违规学生姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="违规事由" prop="reason">
        <el-input v-model="dataForm.reason" placeholder="违规事由"></el-input>
      </el-form-item>
      <el-form-item label="证明图片" prop="confirmImg">
        <!-- <el-input v-model="dataForm.confirmImg" placeholder="证明图片"></el-input> -->
        <single-upload v-model="dataForm.confirmImg"></single-upload>
      </el-form-item>
      <el-form-item label="处理结果" prop="result">
        <el-input v-model="dataForm.result" placeholder="处理结果"></el-input>
      </el-form-item>
      <!-- <el-form-item label="违规日期" prop="createTime">
        <el-input
          v-model="dataForm.createTime"
          placeholder="违规日期"
        ></el-input>
      </el-form-item> -->
      <!-- <el-form-item label="违规人宿舍号" prop="dormId">
      <el-input v-model="dataForm.dormId" placeholder="违规人宿舍号"></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from "@/components/upload/singleUpload";
export default {
  components: {
    SingleUpload,
  },
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        userId: "",
        reason: "",
        confirmImg: "",
        result: "",
        createTime: "",
        dormId: "",
        num: "",
        realName: "",
      },
      dataRule: {
        userId: [
          { required: true, message: "违规学生id不能为空", trigger: "blur" },
        ],
        reason: [
          { required: true, message: "违规事由不能为空", trigger: "blur" },
        ],
        confirmImg: [
          { required: true, message: "证明图片不能为空", trigger: "blur" },
        ],
        result: [
          { required: true, message: "处理结果不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "违规日期不能为空", trigger: "blur" },
        ],
        dormId: [
          { required: true, message: "违规人宿舍号不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/dormviolation/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.userId = data.dormViolation.userId;
              this.dataForm.reason = data.dormViolation.reason;
              this.dataForm.confirmImg = data.dormViolation.confirmImg;
              this.dataForm.result = data.dormViolation.result;
              this.dataForm.createTime = data.dormViolation.createTime;
              this.dataForm.dormId = data.dormViolation.dormId;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/dormviolation/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              reason: this.dataForm.reason,
              confirmImg: this.dataForm.confirmImg,
              result: this.dataForm.result,
              num: this.dataForm.num,
              realName: this.dataForm.realName,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
